<template>
    <div class="app-container">
        <el-dialog v-model="props.visible" title="机台辅料选择" width="70%" @close="handleCancel">
            <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="auto" label-position="left"
                v-show="showSearch">
                <el-row>
                    <el-form-item label="物料名称" prop="materialName">
                        <el-input v-model.trim="queryParams.materialName" clearable style="width: 180px;"
                                  placeholder="请输入物料名称" @keyup.enter="handleQuery" />
                    </el-form-item>
                    <el-form-item label="物料编码" prop="materialCode">
                        <el-input v-model.trim="queryParams.materialCode" clearable style="width: 180px;"
                                  placeholder="请输入物料编码" @keyup.enter="handleQuery" />
                    </el-form-item>
<!--                    <el-form-item label="领用单编码" prop="useCode">-->
<!--                        <el-input v-model.trim="queryParams.useCode" clearable style="width: 180px;"-->
<!--                            placeholder="请输入物料领用单编码" @keyup.enter="handleQuery" />-->
<!--                    </el-form-item>-->
<!--                    <el-form-item label="领用机台" prop="machineName">-->
<!--                        <el-input v-model.trim="queryParams.machineName" clearable style="width: 180px;"-->
<!--                            placeholder="请输入领用机台" @keyup.enter="handleQuery" />-->
<!--                    </el-form-item>-->
<!--                    <el-form-item label="领用部门" prop="deptId">-->
<!--                        <el-tree-select clearable style="width: 200px" v-model="queryParams.deptId" :data="deptOptions"-->
<!--                            :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id"-->
<!--                            placeholder="请选择领用部门" check-strictly />-->
<!--                    </el-form-item>-->
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-row>

            </el-form>
            <div>
                <el-button style="margin-bottom: 5px;" type="primary" plain @click="handleSubmit"
                           :disabled="multiple">确认</el-button>
                <el-table ref="dataListRef" v-loading="loading" :data="dataList" stripe row-key="id" border style="cursor: pointer;"
                          @selection-change="handleSelectionChange"> <!-- @cell-click="handleCellClick"-->
                    <el-table-column type="selection" :reserve-selection="true" align="center" width="65" />
                    <el-table-column label="序号" width="55" type="index" align="center" />
<!--                    <el-table-column label="领用单编码" min-width="180" align="center" prop="useCode"-->
<!--                        :show-overflow-tooltip="true">-->
<!--                        <template v-slot="{ row }">-->
<!--                            <el-button type="primary" text>{{ row.useCode }}</el-button>-->
<!--                        </template>-->
<!--                    </el-table-column>-->
<!--                    <el-table-column label="出库日期" min-width="120" align="center" prop="outDate"-->
<!--                        :show-overflow-tooltip="true">-->
<!--                    </el-table-column>-->
                    <el-table-column label="物料分类" min-width="150" align="center" prop="className"
                        :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column label="物料编码" min-width="190" align="center" prop="materialCode"
                        :show-overflow-tooltip="true">
                        <template v-slot="{ row }">
                            <el-button type="primary" text>{{ row.materialCode
                            }}</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="物料名称" min-width="150" align="center" prop="materialName"
                        :show-overflow-tooltip="true">
                    </el-table-column>
                    <el-table-column label="规格型号" min-width="80" align="center" prop="specs" :show-overflow-tooltip="true">
<!--                        <template #default="scope">-->
<!--                            <dict-tag :options="mes_paper_specs" :value="scope.row.specs" />-->
<!--                        </template>-->
                    </el-table-column>
                    <el-table-column label="单位" min-width="70" align="center" prop="unit" :show-overflow-tooltip="true">
                        <template #default="scope">
                            <dict-tag :options="mes_material_unit" :value="scope.row.unit" />
                        </template>
                    </el-table-column>
                    <el-table-column label="机台库存" min-width="80" align="center" prop="machineStock"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="单价" min-width="100" align="center" prop="price" :show-overflow-tooltip="true">
                    </el-table-column>
<!--                    <el-table-column label="金额" min-width="100" align="center" prop="amount" :show-overflow-tooltip="true">-->
<!--                    </el-table-column>-->
<!--                    <el-table-column label="领用部门" min-width="120" align="center" prop="deptName">-->
<!--                    </el-table-column>-->
<!--                    <el-table-column min-width="120" label="领用机台" align="center" prop="machineName">-->
<!--                    </el-table-column>-->
                    <el-table-column width="120" label="制单人" align="center" prop="createBy" :show-overflow-tooltip="true" />
                    <el-table-column width="100" label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />

                </el-table>
                <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize" @pagination="getList" />
            </div>
        </el-dialog>
    </div>
</template>

<script setup name="searchMaterialRequisition">
import { detailMaterialuse } from '@/api/storage/materialRequisition.js'
import { deptTreeSelect } from "@/api/system/user";
const { proxy } = getCurrentInstance();
const emit = defineEmits(['changeUpdate'])
const { mes_paper_material, mes_paper_specs, mes_paper_brand, mes_material_unit
} = proxy.useDict("mes_paper_material", "mes_paper_specs", "mes_paper_brand", 'mes_material_unit');
const loading = ref(true);
const dataList = ref([])
const showSearch = ref(true);
const router = useRouter()
const total = ref(0);
const deptOptions = ref([])
const selectRow = ref([])
const multiple = ref(true)
const dataListRef =ref(null)
const props = defineProps({
    visible: {
        type: Boolean,
        default: false
    },
    requisitiontList: {
        type: Array,
        default: () => { [] }
    },
    params:{
        type: Object,
        default: () => { { } }
    }
})
watch(() => props.visible, (val) => {
    if (val) {
        handleQuery()
        nextTick(() => {
            dataListRef.value.clearSelection()
            if (props.requisitiontList!=undefined && props.requisitiontList.length > 0) {
                props.requisitiontList.forEach(item => {
                    dataListRef.value.toggleRowSelection(item, true)
                })
            }
        })
    }
})
const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
    },
});




const { queryParams } = toRefs(data);

function getDeptTree() {
    deptTreeSelect().then(response => {
        deptOptions.value = response.data;
    });
};
const getList = () => {
    loading.value = true
    detailMaterialuse({...queryParams.value,...props.params}).then(res => {
        loading.value = false
        dataList.value = res.rows
        total.value = res.total
    }).catch(err => {
        loading.value = false
    })
}
const handleCancel = () => {
    proxy.resetForm("queryRef");
    emit('cancel')
}
const handleCellClick = (row) => {
    proxy.$modal.confirm('是否确认选择「' + row.useCode + '」').then(function () {
        emit('changeUpdate', row)
    }).catch(() => { });
}
// 搜索
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
const resetQuery = () => {
    proxy.resetForm("queryRef");
    handleQuery();
};

// 选中
const handleSelectionChange = (row) => {
    console.log(row, 'row');
    // row.forEach(item => {
    //     item.total = Number(item.quantity)
    // })
    selectRow.value = row
    multiple.value = !row.length
}

const handleSubmit = () => {
    emit('changeUpdate', selectRow.value)
}

getDeptTree()
getList()


</script>

<style lang="scss" scoped>
.operation {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-bottom: 18px;
}
</style>